<template>
  <avue-crud
    :option="option"
    :table-loading="loading"
    :data="data"
    ref="crud"
    v-model="form"
    :search.sync="search"
    @row-del="rowDel"
    :before-open="beforeOpen"
    :page="page"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @refresh-change="refreshChange"
    @on-load="onLoad"
  >
    <template #menuRight="{ row }">
      <el-button type="primary" size="small" @click="addNew"
        >新增推广员</el-button
      >
    </template>
    <template #menu="{ row, index, size }">
      <el-button
        type="text"
        size="small"
        icon="el-icon-edit"
        @click="rowEdit(row, index)"
        >编辑</el-button
      >
      <el-button
        type="text"
        size="small"
        icon="el-icon-delete"
        @click="rowDel(row, index)"
        >删除</el-button
      >
    </template>
  </avue-crud>
</template>

<script>
import { getList, remove } from "@/api/marketing-management/promoters";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      search: {},
      roleBox: false,
      excelBox: false,
      loading: true,
      selectionList: [],
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      props: {
        label: "title",
        value: "key",
      },
      option: {
        columnBtn: false,
        refreshBtn: false,
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: false,
        viewBtn: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        align: "center",
        column: [
          {
            label: "推广员姓名",
            prop: "sharerName",
            span: 24,
            disabled: true,
          },
          {
            label: "手机号码",
            prop: "phone",
            span: 24,
            disabled: true,
          },
          {
            label: "openId",
            prop: "openId",
            span: 24,
          },
          {
            label: "登录账号",
            prop: "userCode",
            span: 24,
          },
        ],
      },
      data: [],
      addDialog: true,
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
  },
  methods: {
    addNew() {
      this.$emit("addNew");
    },
    rowEdit(row, index) {
      this.$emit("editPromoters", row);
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      console.log(type);
      if (type === "edit") {
        this.$emit("editPromoters");
        done();
      }
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
